package cn.cqie.frame.service.impl;

import cn.cqie.frame.bo.add.AddClazzCourseTeacherBo;
import cn.cqie.frame.bo.query.QueryClazzCourseTeacherBo;
import cn.cqie.frame.bo.update.UpdateClazzCourseTeacherBo;
import cn.cqie.frame.entity.ClazzCourseTeacher;
import cn.cqie.frame.mapper.ClazzCourseTeacherMapper;
import cn.cqie.frame.service.ClazzCourseTeacherService;
import cn.cqie.frame.utils.AssertUtil;
import cn.cqie.frame.utils.MyPageHelper;
import cn.cqie.frame.vo.ClazzCourseTeacherVo;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHL
 * @since 2021-10-07
 */
@Service
public class ClazzCourseTeacherServiceImpl extends ServiceImpl<ClazzCourseTeacherMapper, ClazzCourseTeacher> implements ClazzCourseTeacherService {
    @Autowired
    private ClazzCourseTeacherService clazzCourseTeacherService;
    @Autowired
    private ClazzCourseTeacherMapper clazzCourseTeacherMapper;

    /**
     * 新增课表信息
     * @param bo
     * @return
     */
    @Override
    public synchronized ClazzCourseTeacherVo saveCct(AddClazzCourseTeacherBo bo) {
        AssertUtil.isNotNull(bo.getClazzId(),"班级编号不能为空");
        AssertUtil.isNotNull(bo.getCourseId(),"课程编号不能为空");
        AssertUtil.isNotNull(bo.getTeacherId(),"教师编号不能为空");
        LambdaQueryWrapper<ClazzCourseTeacher> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClazzCourseTeacher::getClazzId,bo.getClazzId())
            .eq(ClazzCourseTeacher::getCourseId,bo.getCourseId())
            .eq(ClazzCourseTeacher::getTeacherId,bo.getTeacherId());
        ClazzCourseTeacher clazzCourseTeacher1 = baseMapper.selectOne(lqw);
        AssertUtil.isNull(clazzCourseTeacher1, "该课表行已经存在");
        ClazzCourseTeacher clazzCourseTeacher = new ClazzCourseTeacher();
        clazzCourseTeacher.setClazzId(bo.getClazzId());
        clazzCourseTeacher.setTeacherId(bo.getTeacherId());
        clazzCourseTeacher.setCourseId(bo.getCourseId());
        boolean insert = clazzCourseTeacherService.saveOrUpdate(clazzCourseTeacher);
        AssertUtil.isTrue(insert, "插入课表失败");
        return BeanUtil.toBean(clazzCourseTeacher, ClazzCourseTeacherVo.class);
    }

    /**
     * 更新课表信息
     * @param bo
     * @return
     */
    @Override
    public Boolean updateSchedule(UpdateClazzCourseTeacherBo bo) {
        AssertUtil.isNotNull(bo.getClazzId(),"班级编号不能为空");
        AssertUtil.isNotNull(bo.getCourseId(),"课程编号不能为空");
        AssertUtil.isNotNull(bo.getTeacherId(),"教师编号不能为空");
        LambdaQueryWrapper<ClazzCourseTeacher> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClazzCourseTeacher::getClazzId,bo.getClazzId())
                .eq(ClazzCourseTeacher::getCourseId,bo.getCourseId())
                .eq(ClazzCourseTeacher::getTeacherId,bo.getTeacherId())
                .eq(ClazzCourseTeacher::getId,bo.getId());
        ClazzCourseTeacher clazzCourseTeacher1 = baseMapper.selectOne(lqw);
        AssertUtil.isNull(clazzCourseTeacher1, "该课表行已经存在");
        LambdaUpdateWrapper<ClazzCourseTeacher> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(bo.getClazzId()!=0,ClazzCourseTeacher::getClazzId,bo.getClazzId())
                            .set(bo.getCourseId()!=0,ClazzCourseTeacher::getCourseId,bo.getCourseId())
                            .set(bo.getTeacherId()!=0,ClazzCourseTeacher::getTeacherId,bo.getTeacherId())
                            .eq(ClazzCourseTeacher::getId,bo.getId());
        int update = baseMapper.update(null, lambdaUpdateWrapper);
        AssertUtil.isTrue(update > 0, "更新课表失败");
        return update >0;
    }

    /**
     * 分页查询课表信息
     * @param bo
     * @return
     */
    @Override
    public List<ClazzCourseTeacherVo> list(QueryClazzCourseTeacherBo bo) {
        MyPageHelper.startPage(bo);
        List<ClazzCourseTeacherVo> clazzCourseTeachers = clazzCourseTeacherMapper.listSchedule(bo);
        return entity2Vo(clazzCourseTeachers);
    }

    /**
     * 删除课表信息
     * @param id
     * @return
     */
    @Override
    public Boolean deleteSchedule(Integer id) {
        boolean b = clazzCourseTeacherService.removeById(id);
        return b;
    }

    /**
     * 格式转换
     * @param clazzCourseTeachers
     * @return
     */
    private List<ClazzCourseTeacherVo> entity2Vo(List<ClazzCourseTeacherVo> clazzCourseTeachers) {
        List<ClazzCourseTeacherVo> voList = clazzCourseTeachers.stream()
                .map(any -> BeanUtil.toBean(any, ClazzCourseTeacherVo.class))
                .collect(Collectors.toList());
        if (clazzCourseTeachers instanceof Page) {
            Page<ClazzCourseTeacherVo> page = (Page<ClazzCourseTeacherVo>) clazzCourseTeachers;
            Page<ClazzCourseTeacherVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }
}
